//
//  HZTabBar.swift
//  weibo002
//
//  Created by 黑眼圈 on 16/4/16.
//  Copyright © 2016年 heiyan. All rights reserved.
//

import UIKit

class HZTabBar: UITabBar {

 
    //定义一个闭包
    var addBtn:(()->())?
    

    //创建按钮
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //添加加号按钮
        addSubview(btn)
        
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //设置按钮的位置居中
        btn.center = CGPoint(x: self.frame.size.width*0.5, y: self.frame.size.height*0.5)
        
        var num = 0
        let wight = UIScreen.mainScreen().bounds.size.width/5
        
        //遍历子控件找到4个标题
        for thing in subviews{
            
            //判断找到想要的子控件
            if thing.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
//                print(thing)
                thing.frame.origin.x = wight*CGFloat(num)
                //设置宽度
                thing.frame.size.width = wight
                
                num++
                
                if num==2 {
                    num++
                }
              
            }
        }
        
    }
    
    
    
    //懒加载加号按钮
    lazy var btn:UIButton = {
        
        
        
        let btn = UIButton()
        
        //监听按钮点击
        btn.addTarget(self, action: "didClick", forControlEvents: .TouchUpInside)
        
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        //让按钮和图片一样大
        btn.sizeToFit()
        
        
        return btn
        
    }()
    
    
    //实现按钮点击
    func didClick(){
        
//        print("下雨了")
        
        //回调闭包
        addBtn!()
        
    }
    

}
